package study.lxy.spring.springboot2.mq.receiver;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;
import study.lxy.spring.springboot2.dao.TransLogDao;
import study.lxy.spring.springboot2.entity.TransLog;
import study.lxy.spring.springboot2.utils.consts.BusiFunction;

import javax.annotation.Resource;

@Component
@RabbitListener(queues = "login.log",containerFactory = "singleListenerContainer")
public class LoginLogReceiver {
    private static final Logger logger = LoggerFactory.getLogger(LoginLogReceiver.class);
    @Resource
    private TransLogDao transLogDao;
    @RabbitHandler
    public void saveLoginLog(String jsonStr){
        logger.info("reciver message is {}", jsonStr);
        TransLog log = buildTransLog(jsonStr,BusiFunction.USER_LOGIN);
        transLogDao.saveTransLog(log);

    }
    private TransLog buildTransLog(String message, BusiFunction func){
        TransLog log = new TransLog();
        log.setBusiCode(func.getCode());
        log.setBusiDesc(func.getDesc());
        log.setTranMessage(message);
        return log;
    }
}
